package com.study.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.hash.Hash;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.R;
import com.study.dto.MyPage;
import com.study.dto.SchoolCounties;
import com.study.dto.UserInformation;
import com.study.entity.Role;
import com.study.entity.School;
import com.study.entity.User;
import com.study.mapper.RoleMapper;
import com.study.mapper.SchoolMapper;
import com.study.mapper.UserMapper;
import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.enterprise.inject.spi.Bean;
import java.util.LinkedList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private RoleMapper roleMapper;

    
    
    @Override
    public List<User> selects() {
           return userMapper.selects();
    }

    @Override
    public int deleteByPrimaryKey(Long userId) {
        return userMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public int insert(User row) {
        return userMapper.insert(row);
    }

    @Override
    public int insertSelective(User row) {
        return userMapper.insertSelective(row);
    }

    @Override
    public User selectByPrimaryKey(Long userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int updateByPrimaryKeySelective(User row) {
        return userMapper.updateByPrimaryKeySelective(row);
    }

    @Override
    public int updateByPrimaryKey(User row) {
        return userMapper.updateByPrimaryKey(row);
    }

    @Override
    public List<UserInformation> selectAll(UserInformation userInformation) {
//        return userMapper.selectAll(userInformation);

        List<User> selects = userMapper.selects();
        List<UserInformation> list = new LinkedList<>() ;
        selects.forEach(item -> {
            UserInformation userInformation1 = new UserInformation();
            if(item.getUserId()!=null){
                User user = userMapper.selectByPrimaryKey(item.getUserId());
                BeanUtil.copyProperties(user,userInformation1);
            }
            // 查学校
            if(item.getSchoolId() != null) {
            School school = schoolMapper.selectByPrimaryKey(item.getSchoolId());
            BeanUtil.copyProperties(school,userInformation1);}
            // 查角色
            if(item.getRoleId() != null){
                Role role = roleMapper.selectByPrimaryKey(item.getRoleId());
                BeanUtil.copyProperties(role,userInformation1);
            }
            list.add(userInformation1);
        });

        return list;
    }


}
